
/*
 * WFSensor.c
 *
 *  Created on: Nov 5, 2021
 *      Author: ITry
 */
#include "WFSensor.h"

/* 传感器的通讯总线地址 */
#define DeviceAddress 0xDA //传感器的设备地址+默认写

/* 驱动程序的状态 */
typedef struct {
	  sensor_model				model;
	  press_measurement_unit		pressUnit;
    bus_mode					busMode;
    const bus_handler	*bus_handler;
    
    /* 以下都是临时变量，无实质意义 */
    uint8_t						arrData[5];
    uint32_t					rawData;
    wf_t						factor;
} wf_state;

/* 定义变量 */
wf_state drv_state= {0};

#define UseDefaultParam 0
/*=============================================================================
---函数：sensor_init(sensor_model model, press_measurement_unit unit, bus_mode mode, const bus_handler* handler)
---功能：传感器初始化配置
---输入：model		- 传感器型号
		 unit		- 压力单位
         mode		- 通讯方式
         handler	- 通讯总线接口
---输出：通讯总线的返回结果
-----------------------------------------------------------------------------*/
int sensor_init(sensor_model model, press_measurement_unit unit, bus_mode mode, const bus_handler* handler) {

	drv_state.model = model;
	drv_state.pressUnit = unit;
	drv_state.busMode = mode;
	drv_state.bus_handler = handler;
	drv_state.bus_handler->wf_delayMs(20);
	return drv_state.bus_handler->write_byte(drv_state.bus_handler->bus_param, DeviceAddress, 0x00, (mode == SPI_4WIRE ? 0x01 : 0x00));
}

/*=============================================================================
---函数：press_unit_factor(void)
---功能：传感器压力计量单位换算
---输入：
---输出：转换关系系数值
-----------------------------------------------------------------------------*/
wf_t press_unit_factor(void){

	//	switch(press_measurement_unit){
	press_measurement_unit u = drv_state.pressUnit;
	wf_t n = 1;
	if(u== bar) n=0.01;
	else if(u== kPa) n=1;
	else if(u== hPa) n=10;
	else if(u== mbar) n=10;
	else if(u== Pa) n=1000;
	else if(u== PSI) n=0.14503774;
	else if(u== kgf_cm2) n=0.010197162;
	else if(u== Torr) n=7.5006168;
	return n;
}

/*=============================================================================
---函数：analyze_press_data(void)
---功能：传感器各型号的压力算法
---输入：
---输出：按初始化的压力单位输出压力值
-----------------------------------------------------------------------------*/
wf_t analyze_press_data(void){

//	switch(sensor_model){
	sensor_model m = drv_state.model;
	wf_t n = drv_state.factor;
	wf_t u = press_unit_factor();
	if(m == WF5803F_1BAR) return (125 * n + 17.5) * u;
	else if(m == WF5803F_2BAR) return (180 / 0.81 * (n - 0.1) + 30) * u;
	else if(m == WF5803F_7BAR) return (1000 * n - 50) * u;
	else if(m == WF100D_5KPA) return (5 * n + 0.25) * u;
	else if(m == WF100D_10KPA) return (15 * n + 2) * u;
	else if(m == WF100D_40KPA) return (50 * n + 5) * u;
	else if(m == WF100D_100KPA) return (125 * n -12.5) * u;
	else if(m == WF100D_200KPA) return (250 * n +25) * u;
	else if(m == WF100D_300KPA) return (400 * n +10) * u;
	else if(m == WF200D_5KPA) return (5 * n + 0.25) * u;
	return 0;
}

/*=============================================================================
---函数：analyze_press_data(void)
---功能：翻转数组
---输入：
---输出：
-----------------------------------------------------------------------------*/
void reverse(uint8_t *p , uint8_t size)
{
    uint8_t i, tmp;
    for(i = 0; i < size/2 ; i++)
    {
         tmp = p[i];
         p[i] = p[size-1-i];
         p[size-1-i] = tmp;
    }
}

/*=============================================================================
---函数：sensor_sampling_all(sensor_data* data)
---功能：同时采样温度以及压力
---输入：data	- 采样数据样本
---输出：
-----------------------------------------------------------------------------*/
void sensor_sampling_all(sensor_data* data) 
{
	if(UseDefaultParam) {//兼容模式，非必要
		drv_state.bus_handler->write_byte(drv_state.bus_handler->bus_param, DeviceAddress, 0xA5, 0x11);
		drv_state.bus_handler->write_byte(drv_state.bus_handler->bus_param, DeviceAddress, 0xA6, 0x28);
		drv_state.bus_handler->write_byte(drv_state.bus_handler->bus_param, DeviceAddress, 0xA7, 0xC0);
	}
	drv_state.bus_handler->write_byte(drv_state.bus_handler->bus_param, DeviceAddress, 0x30, 0x0A);
	drv_state.bus_handler->wf_delayMs(20);
	drv_state.bus_handler->read_bytes(drv_state.bus_handler->bus_param, DeviceAddress, 0x02, 1, &data->status);
	if(drv_state.busMode == SPI_3WIRE || drv_state.busMode == SPI_4WIRE) {
		drv_state.bus_handler->read_bytes(drv_state.bus_handler->bus_param, DeviceAddress, 0x0A, 5, drv_state.arrData);
		reverse(drv_state.arrData, 5);
	}
	else{
		drv_state.bus_handler->read_bytes(drv_state.bus_handler->bus_param, DeviceAddress, 0x06, 5, drv_state.arrData);
	}
	drv_state.rawData = drv_state.arrData[0];
    drv_state.rawData = drv_state.rawData << 8;
    drv_state.rawData |= drv_state.arrData[1];
    drv_state.rawData = drv_state.rawData << 8;
    drv_state.rawData |= drv_state.arrData[2];
    if (drv_state.rawData > 8388608) {
     	drv_state.factor = (int32_t)(drv_state.rawData - 16777216) / 8388608.0;
    } else {
      	drv_state.factor = drv_state.rawData / 8388608.0;
    }
    data->pressure = analyze_press_data();
    drv_state.rawData = drv_state.arrData[3];
    drv_state.rawData = drv_state.rawData << 8;
    drv_state.rawData |= drv_state.arrData[4];
    if (drv_state.rawData > 32768) {
      data->temperature = (drv_state.rawData - 65536) / 256.0;
    }
    else {
      data->temperature = drv_state.rawData / 256.0;
    }
}
/******************************************************************************************************************************************/
